$(function () {
    //表格初始加载
    $('#grid_byif').datagrid({
        url:$.webPath() + "/kcgl/sskc/byifList",
        toolbar:"#toolbar_byif",
        fitColumns:false,
        singleSelect:true,
        pagination:true,
        pageNumber:1,
        fit:true,
        checkOnSelect:false,
        selectOnCheck:false,
        rownumbers:true,
        striped:true,
        showFooter:true,
        frozenColumns:[
            [
                {field:'QC', align:'center', title:'代理客户'},
                {field:'NAME', align:'center', title:'物资名称'},
                {field:'GGXH', align:'center', title:'规格型号'},
                {field:'CODE', align:'center', title:'物资编码'},
                {field:'DW', align:'center', title:'单位'},
                {field:'Z', align:'center', title:'产地'}
            ]
        ],
        columns:[
            [
                {field:'sbjs', align:'right', title:'申报件数', width:100,
                    formatter:function (value) {
                        var val = formatSlForHtml(value);
                        return val;
                    }},
                {field:'sbsl', align:'right', title:'申报数量', width:100,
                    formatter:function (value) {
                        var val = formatSlForHtml(value);
                        return val;
                    }},
                {field:'ckjs', align:'right', title:'出库件数', width:100,
                    formatter:function (value) {
                        var val = formatSlForHtml(value);
                        return val;
                    }},
                {field:'cksl', align:'right', title:'出库数量', width:100,
                    formatter:function (value) {
                        var val = formatSlForHtml(value);
                        return val;
                    }},

                {field:'syjs', align:'right', title:'待分配件数', width:100,
                    formatter:function (value) {
                        var val = formatSlForHtml(value);
                        return val;
                    }},
                {field:'sysl', align:'right', title:'待分配数量', width:100,
                    formatter:function (value) {
                        var val = formatSlForHtml(value);
                        return val;
                    }},
                {field:'gdjs', align:'right', title:'挂单件数', width:100,
                    formatter:function (value) {
                        var val = formatSlForHtml(value);
                        return val;
                    }},
                {field:'gdsl', align:'right', title:'挂单数量', width:100,
                    formatter:function (value) {
                        var val = formatSlForHtml(value);
                        return val;
                    }}
            ]
        ],
        onLoadSuccess:function () {
            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        }
    });

    //**********按钮触发事件**********
    //查询按钮
    $("#btn_byif_search").click(function () {
        byif_search();
    });

    // 图表按钮
    $("#btn_byif_list").click(function () {
        byif_chart();
    });
});

// 客户库存列表
function byif_list() {
    document.getElementById("layout_byif").style.display = ""
    document.getElementById("div_byif_container").style.display = "none";
}

// 客户物资与库存图表
function byif_chart() {
    var gysName = $('#byif_gys').val() == null ? null : $('#byif_gys').val();
    var wzmc = $('#byif_wzmc').val() == null ? null : $('#byif_wzmc').val();
    var wzcode = $('#byif_code').val() == null ? null : $('#byif_code').val();
    var wzggxh = $('#byif_ggxh').val() == null ? null : $('#byif_ggxh').val();
    $.ajax({
        type:"POST",
        url:encodeURI(encodeURI($.webPath() + "/kcgl/sskc/byifChart?gysName=" + gysName + "&code=" + wzcode + "&wzmc=" + wzmc + "&ggxh=" + wzggxh + "")),
        dataType:"text",
        beforeSend:function () {
            $.messager.progress();
        },
        success:function (data) {
            var dataJson = $.parseJSON(data);
            $.messager.progress("close");
            if (null != dataJson.listXdata && null != dataJson.sbsl) {
                $('#byif_container').highcharts({
                    chart:{
                        type:'column'
                    },
                    title:{
                        text:'商权货值统计表'
                    },
                    xAxis:{
                        categories:dataJson.listXdata,
                        labels:{
                            rotation:-45,
                            align:'right',
                            style:{
                                fontSize:'13px',
                                fontFamily:'Verdana, sans-serif'
                            }
                        }
                    },
                    yAxis:{
                        min:0,
                        title:{
                            text:'Total fruit consumption'
                        },
                        stackLabels:{
                            enabled:true,
                            style:{
                                fontWeight:'bold',
                                color:(Highcharts.theme && Highcharts.theme.textColor) || 'gray'
                            }
                        }
                    },
                    legend:{
                        align:'right',
                        x:-70,
                        verticalAlign:'top',
                        y:20,
                        floating:true,
                        backgroundColor:(Highcharts.theme && Highcharts.theme.legendBackgroundColorSolid) || 'white',
                        borderColor:'#CCC',
                        borderWidth:1,
                        shadow:false
                    },
                    tooltip:{
                        formatter:function () {
                            return '<b>' + this.x + '</b><br/>' +
                                this.series.name + ': ' + this.y + '<br/>' +
                                'Total: ' + this.point.stackTotal;
                        }
                    },
                    plotOptions:{
                        column:{
                            stacking:'normal',
                            dataLabels:{
                                enabled:true,
                                color:(Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white'
                            }
                        }
                    },
                    series:[
                        {
                            name:'未分配数量',
                            data:dataJson.wfpsl
                        },
                        {
                            name:'已分配数量',
                            data:dataJson.rksl
                        }
                    ]
                });
            }

        }
    });
    document.getElementById("layout_byif").style.display = "none"
    document.getElementById("div_byif_container").style.display = "";
}

//实时库存查询
function byif_search() {
    var gysName = $('#byif_gys').val() == null ? null : $('#byif_gys').val();
    var wzmc = $('#byif_wzmc').val() == null ? null : $('#byif_wzmc').val();
    var wzcode = $('#byif_code').val() == null ? null : $('#byif_code').val();
    var wzggxh = $('#byif_ggxh').val() == null ? null : $('#byif_ggxh').val();
    $('#grid_byif').datagrid('load',
        {
            gysName:gysName,
            code:wzcode,
            wzmc:wzmc,
            ggxh:wzggxh
        }
    );
}



